//
//  EpointHomeViewController.m
//  EpointFrame5
//
//  Created by wangzhigang on 14-8-6.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointHomeViewController.h"
#import "HomeTodoCell.h"
#import "EpointUnHandleListModel.h"
#import "HomeMailCell.h"
#import "MailInfo.h"
#import "HomeWebInfoCell.h"
#import "ScheduleInfo.h"
#import "EpointWebInfoModel.h"
#import "HomeScheduleCell.h"

#define SynTaskId 1234

@interface EpointHomeViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray*todoDataArray,*mailDataArray,*webinfoDataArray,*scheduleDataArray;
    NSIndexPath* todoIndexPath;
}

@end

@implementation EpointHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.todoListView setContentOffset:CGPointMake(CGFLOAT_MAX, CGFLOAT_MAX)];
    // Do any additional setup after loading the view from its nib.
    NSString *clientDisplayName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    self.subjectTitle.text =clientDisplayName;
}

#pragma mark 获取待办列表
-(void)getDBData
{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"" forKey:@"HandleType"];
    [params setObject:@"" forKey:@"keyword"];
    [params setObject:[NSString stringWithFormat:@"%i",1] forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",20] forKey:@"pagesize"];
    [self startTask:@"Get_Handle_GetUnHandleList_Task" withParams:params withTaskId:0];
}

#pragma mark 获取邮件列表
- (void)getMailData {
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[NSString stringWithFormat:@"%d", 20] forKey:@"pagesize"];
    [params setObject:@"" forKey:@"keyword"];
    [params setObject:[NSString stringWithFormat:@"%d", 1] forKey:@"CurrentPageIndex"];
   
    // TODO: get inbox
    [params setObject:InboxType forKey:@"boxType"];
    [self startTask:@"Mail_GetList_Task" withParams:params withTaskId:1];

}

#pragma mark 获取信息简报
-(void)getWebinfoData
{

    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[NSString stringWithFormat:@"%i",20] forKey:@"pagesize"];
    [params setObject:@"" forKey:@"keyword"];
    [params setObject:@"fa16ce80-df9c-486d-bfc4-4f784bef964b" forKey:@"categoryguid"];
    [params setObject:[NSString stringWithFormat:@"%i",1] forKey:@"CurrentPageIndex"];
    [self startTask:@"WebInfo_GetView_Tsk_c" withParams:params withTaskId:2];
}
#pragma mark 获取日程列表
- (void)getScheduleData {
    NSString *year = [self getCurrentYear];
    
    NSString *FromDateTime = [NSString stringWithFormat:@"%@-%@", year, @"01-31"];
    NSString *ToDateTime = [NSString stringWithFormat:@"%@-%@", year, @"12-31"];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:FromDateTime forKey:@"FromDateTime"];
    [params setObject:ToDateTime forKey:@"ToDateTime"];
    [params setObject:@"" forKey:@"KeyWord"];
    [self startTask:@"Schedule_GetList_Task" withParams:params withTaskId:3];
}
- (NSString *)getCurrentYear {
    return [EpointDateUtils convertDate2FormatStr:[NSDate date] format:@"yyyy"];
}

//数据同步
-(void)synData:(EpointPhoneViewController *)vc
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *syndate = [ud objectForKey:Epoint_Syn_Date];
    if (syndate==nil||syndate.length==0) {
        [self startTask:@"EpointSynTask" withParams:nil withTaskId:SynTaskId];
    }else{
         [self hideMBPHUD];
    }
}


-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    if(taskId==SynTaskId){
        [self hideMBPHUD];
        return;
    }
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        [self hideMBPHUD];
        return;
    }
    if (taskId==0)
    {
        todoDataArray = [[[[arr objectAtIndex:2] reverseObjectEnumerator] allObjects] mutableCopy];
        [self.todoListView reloadData];
        [self getMailData];
        return;
    }else if(taskId==1){
        mailDataArray = [[arr objectAtIndex:2] mutableCopy];
        [self.mailListView reloadData];
        [self getWebinfoData];
        return;
    }else if(taskId==2){
        webinfoDataArray = [[arr objectAtIndex:2] mutableCopy];
        [self.webinfoListView reloadData];
        [self getScheduleData];
        return;
    }else if(taskId==3){
        scheduleDataArray = [[[[arr objectAtIndex:2]reverseObjectEnumerator] allObjects] mutableCopy];
        [self.scheduleListView reloadData];
        [self synData:self];
        return;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==0) {
        return todoDataArray.count;
    }else if(tableView.tag==1){
        return mailDataArray.count;
    }else if(tableView.tag==2){
        return webinfoDataArray.count;
    }else if(tableView.tag==3){
        return scheduleDataArray.count;
    }
    return 0;
}
-(void)viewWillAppear:(BOOL)animated{
    [self getDBData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag==0) {
        static NSString *identifier = @"HomeTodoCell";
        HomeTodoCell*cell =(HomeTodoCell*) [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            NSString *nibName = @"HomeTodoCell";
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:nibName owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.backgroundColor = [UIColor clearColor];
        }
        EpointUnHandleListModel* model = [todoDataArray objectAtIndex:indexPath.row];
        NSString* subject = model.Title.length>10?[NSString stringWithFormat:@"%@...",[model.Title substringToIndex:10]]:model.Title;
        cell.homeTitle.text =[NSString stringWithFormat:@"%@  %@",subject,model.GenerateDate];
        if (indexPath.row==todoDataArray.count-1) {
            cell.homeTitle.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
        }
        return cell;
        
    }else if(tableView.tag==1){
        
        static NSString *identifier = @"HomeMailCell";
        HomeMailCell*cell =(HomeMailCell*) [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            NSString *nibName = @"HomeMailCell";
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:nibName owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.backgroundColor = [UIColor clearColor];
        }
        MailInfo* model = [mailDataArray objectAtIndex:indexPath.row];
        cell.homeTitle.text =model.Subject;
        cell.homeTime.text = model.SENDTIME;
        return cell;
        
    }else if(tableView.tag==2){
        
        static NSString *identifier = @"HomeWebInfoCell";
        HomeWebInfoCell*cell =(HomeWebInfoCell*) [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            NSString *nibName = @"HomeWebInfoCell";
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:nibName owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.backgroundColor = [UIColor clearColor];
        }
        EpointWebInfoModel* model = [webinfoDataArray objectAtIndex:indexPath.row];
        cell.homeTitle.text =model.txtTitle;
        cell.homeTime.text = model.PostDate;
        return cell;
        
    }else if(tableView.tag==3){
        static NSString *identifier = @"HomeScheduleCell";
        HomeScheduleCell*cell =(HomeScheduleCell*) [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            NSString *nibName = @"HomeScheduleCell";
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:nibName owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.backgroundColor = [UIColor clearColor];
        }
        ScheduleInfo* model = [scheduleDataArray objectAtIndex:indexPath.row];
        NSString* subject = model.Subject.length>10?[NSString stringWithFormat:@"%@...",[model.Subject substringToIndex:10]]:model.Subject;
        cell.homeTitle.text =[NSString stringWithFormat:@"%@  %@",subject,model.FromDateTime];
        if (indexPath.row==0) {
            cell.homeTitle.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
        }
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==0) {
        XAppDelegate.clickPos=1;
         XAppDelegate.clickItemPos=todoDataArray.count-indexPath.row-1;
    }else if(tableView.tag==1){
        XAppDelegate.clickPos=2;
        XAppDelegate.clickItemPos=indexPath.row;
    }else if(tableView.tag==2){
        XAppDelegate.clickPos=3;
        XAppDelegate.clickItemPos=indexPath.row;
    }else if(tableView.tag==3){
        XAppDelegate.clickPos=6;
        ScheduleInfo* model = [scheduleDataArray objectAtIndex:indexPath.row];
        XAppDelegate.clickScheduleAtHome = model;
        XAppDelegate.clickItemPos = indexPath.row;
    }
    [XAppDelegate initPPSView];
    [self presentViewController:XAppDelegate.stackController animated:YES completion:nil];
}
- (IBAction)clickAction:(id)sender {
    UIView* v = (UIView*)sender;
    XAppDelegate.clickPos=v.tag+1;
    XAppDelegate.clickItemPos=-1;
    [XAppDelegate initPPSView];
     [self presentViewController:XAppDelegate.stackController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
